<template>
  <div>
  <attachment
    :batch-upload="editable"
    :hidden="true"
    :auto-upload="false"
    :show="false"
    :multiple="true"
    @onFileSuccess="onFileSuccess(arguments)"
  />
  <base-table :columns="archiveColumns"
              :props-init="archiveInit"
              :props-save="archiveSave"
              :props-delete="archiveDelete"
              :data="archiveData"
              :rules="archiveRules"
              ref="baseTable"
              :editable="editable"
  />
  </div>
  <!-- :props-delete="archiveDelete"  -->
</template>

<script>
import BaseTable from '@/components/baseTable'
import Attachment from '@/components/ScAttachment'
import {
  addAfterRentFile,
  deleteAfterRentFile,
  listAfterRentFile,
  updateAfterRentFile
} from "@/api/hsrzzl/afterRent/archives";
export default {
  name: 'ArchiveTable',
  components: {
    Attachment,
    BaseTable
  },
  props: {
    clientId: {
      type: Number,
      required: false,
      default: -1
    },
    projectId: {
      type: Number,
      required: false,
      default: -1
    },
    contractId: {
      type: Number,
      required: false,
      default: -1
    },
    paymentId: {
      type: Number,
      required: false,
      default: -1
    },
    stage: {
      type: String,
      required: false,
      default: null
    },
    sortNumber: {
      type: Number,
      required: false,
      default: null
    },
    editable:{
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      archiveData: [],
      archiveColumns: [
        {
          prop: 'attachmentName',
          label: '附件名称',
          type: 'INPUT'
        },
        {
          prop: 'attachmentId',
          label: '上传文件',
          type: 'UPLOAD'
        }
      ],
      archiveRules: {
        attachmentName: [
          { required: true, message: '附件名称不能为空', trigger: 'blur' },
          { pattern: /^[\s\S]{1,64}$/, message: '最大不能超过64个字', trigger: 'blur' },
        ],
        attachmentId: [
          { required: true, message: '附件不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch:{
    clientId(val, oldVal){//普通的watch监听
      this.archiveInit()
    },
    projectId(val, oldVal){//普通的watch监听
      this.archiveInit()
    },
    contractId(val, oldVal){//普通的watch监听
      this.archiveInit()
    },
    paymentId(val, oldVal){//普通的watch监听
      this.archiveInit()
    },
  },
  methods: {
    archiveInit() {
      let flag = true;
      let queryParam = {}
      if(this.clientId !== -1) {
        queryParam.clientId = this.clientId
        flag = false;
      }
      if(this.projectId !== -1) {
        queryParam.projectId = this.projectId
        flag = false;
      }
      if(this.contractId !== -1) {
        queryParam.contractId = this.contractId
        flag = false;
      }
      if(this.paymentId !== -1) {
        queryParam.paymentId = this.paymentId
        flag = false;
      }
      if(!flag) {
        listAfterRentFile(queryParam).then(res => {
          this.archiveData = res.rows
        })
      }
    },
    archiveDelete(id) {
      return deleteAfterRentFile(id)
    },
    onFileSuccess(list) {
      let _this = this;
      list[1].forEach(item => {
        let row = {
          attachmentName: item.raw.name,
          attachmentId: item.response.attachmentId
        }
        _this.archiveSave(row, true)
      })
      let timer;
      clearTimeout(timer);
      _this.$nextTick(() => {
        timer = setTimeout(function () {
          _this.$refs.baseTable.reload()
        }, 500);
      })
    },
    archiveSave(row, notReloadFlag) {
      let flag = true;
      if(this.clientId !== -1) {
        this.$set(row, 'clientId', this.clientId)
        flag = false;
      }
      if(this.projectId !== -1) {
        this.$set(row, 'projectId', this.projectId)
        flag = false;
      }
      if(this.contractId !== -1) {
        this.$set(row, 'contractId', this.contractId)
        flag = false;
      }
      if(this.paymentId !== -1) {
        this.$set(row, 'paymentId', this.paymentId)
        flag = false;
      }
      if(this.stage){
        this.$set(row, 'stage', this.stage)
      }
      if(this.sortNumber){
        this.$set(row, 'sort', this.sortNumber)
      }
      if(!flag) {
        if (row.id !== undefined) {
          return updateAfterRentFile(row).then(res =>{
            if(notReloadFlag){

            }else{
              this.$refs.baseTable.reload()
            }
          })
        } else {
          return addAfterRentFile(row).then(res =>{
            if(notReloadFlag){

            }else{
              this.$refs.baseTable.reload()
            }
          })
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
